#include "Config.h"
#include "CryptoSystem.h"

#include <string>

void PrintCmdHelp()
{
	fprintf(stdout, "Barter Server version 1.0\n");
	fprintf(stdout, "BarterServer [options]\n");
	fprintf(stdout, "\n");
	fprintf(stdout, "\t--config-file (-c) fileName\tUse an alternate config file\n");
	fprintf(stdout, "\t--config-dir (-d) fileName \tUse an alternate config directory\n");
	fprintf(stdout, "\t--help (-?)                \tShow this help message\n");
}

int main(int argc, const char** argv)
{
	std::string configFile = "barter.conf";
	std::string confDir = "./";
	if (argc > 2)
	{
		std::string cmd = "";
		for (int z = 1; z < argc; z++)
		{
			cmd = argv[i];
			if (cmd == "-c" || cmd == "--config-file")
			{
				configFile = argv[z + 1];
				z++;
			}
			else if (cmd == "-d" || cmd == "--config-dir")
			{
				configFile = argv[z + 1];
			}
			else if (cmd == "-?" || cmd == "--help")
			{
				PrintCmdHelp();
			}
			else
			{
				fprintf(stdout, "Barter Server: Unknown command line option `%s`\n", cmd.c_str());
				PrintCmdHelp();
			}
		}
	}
	char e = confDir[confDir.length() - 1];
	if (!(e == '/' || e == '\\')) // ensure the config directory has a trailing slash
	{
		confDir += "/";
	}
	if (confDir[confDir.length() - 1] != '/'
	fprintf(stdout, "Barter Server: Loading server config file `%s`\n", configFile.c_str());
	conf.ParseConfigFile(confDir + configFile);
	fprintf(stdout, "Initializing CryptoSystem\n");
	crypto.Init();
	fprintf(stdout, "Initializing DBManager\n");
	dbMan.Init();
	fprintf(stdout, "Initializing NetworkSystem\n");
	netSys.Init();
	fprintf(stdout, "Starting NetworkSystem\n");
	netSys.Run();
}